package com.mxgraph.examples.swing;

import java.awt.Color;

import org.shortpasta.icmp2.IcmpPingUtil;
import org.shortpasta.icmp2.IcmpPingRequest;
import org.shortpasta.icmp2.IcmpPingResponse;
import org.shortpasta.icmp2.util.ArgUtil;

import com.mxgraph.examples.swing.editor.ArrayListofVisibleItem;
import com.mxgraph.examples.swing.editor.DatabaseConnectivity;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxICell;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxGraph;
import java.net.InetAddress;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 * ShortPasta Foundation http://www.shortpasta.org Copyright 2009 and beyond,
 * Sal Ingrilli at the ShortPasta Software Foundation
 * <p/>
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License version 3 as published by the
 * Free Software Foundation as long as: 1. You credit the original author
 * somewhere within your product or website 2. The credit is easily reachable
 * and not burried deep 3. Your end-user can easily see it 4. You register your
 * name (optional) and company/group/org name (required) at
 * http://www.shortpasta.org 5. You do all of the above within 4 weeks of
 * integrating this software 6. You contribute feedback, fixes, and requests for
 * features
 * <p/>
 * If/when you derive a commercial gain from using this software please donate
 * at http://www.shortpasta.org
 * <p/>
 * If prefer or require, contact the author specified above to: 1. Release you
 * from the above requirements 2. Acquire a commercial license 3. Purchase a
 * support contract 4. Request a different license 5. Anything else
 * <p/>
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE, similarly to how this is described in the GNU
 * Lesser General Public License.
 * <p/>
 * User: Sal Ingrilli Date: May 23, 2014 Time: 6:43:43 PM
 */
public class PingIpCheck implements Runnable {

    GraphEditor editor;
    String ipAddress;

    public PingIpCheck(GraphEditor editor) {
        this.editor = editor;
    }

    /**
     * The Java interface
     *
     * To test: cd
     * c:\dev\shortpasta\trunk\icmp2\output\publish\shortpasta-icmp2\lib java
     * -cp * org.shortpasta.icmp2.tool.Ping www.google.com
     *
     * @param args
     */
    @Override
    public void run() {
        Statement stmt = DatabaseConnectivity.generateStatement();
//    stmt = 
        String sqlForSwitchIP = ("SELECT ip FROM packetfence_manuallyAddswitch_info");
        String sqlForAnotherSwitchIP = ("SELECT ip FROM packetfence_switch_info");
        String sqlForServerIP = ("SELECT ip FROM packetfence_server_info");
        String sqlForPrinterIP = ("SELECT ip FROM packetfence_printer_info");
        String sqlForRouterIP = ("SELECT ips FROM packetfence_router_info");
        String sqlForFirewallIP = ("SELECT ips FROM packetfence_firewall_info");
        mxGraph graph = editor.getGraphComponent().getGraph();
        ArrayList<String> switchIP = new ArrayList<String>();
        ArrayList<String> switchAnotherIP = new ArrayList<String>();
        ArrayList<String> serverIP = new ArrayList<String>();
        ArrayList<String> printerIP = new ArrayList<String>();
        ArrayList<String> routerIP = new ArrayList<String>();
        ArrayList<String> firewallIP = new ArrayList<String>();
        //String[] switchIP = null;
        //String[] switchAnotherIP = null ;
        //String[] serverIP = null;
        //String[] printerIP = null;
        //String[] routerIP = null;
        //String[] firewallIP = null;

        //try {
        while (true) {
            System.out.println("PingThread is Alive");
            int ipCounter = 0;
            ResultSet rs;
            try {
                rs = stmt.executeQuery(sqlForSwitchIP);
                while (rs.next()) {
                    switchIP.add(rs.getString("ip"));
                    ipCounter++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                rs = stmt.executeQuery(sqlForAnotherSwitchIP);
                while (rs.next()) {
                    switchAnotherIP.add(rs.getString("ip"));
                    System.out.println("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW");
                    System.out.println("switchAnotherIP[ipCounter]:" + switchAnotherIP.get(ipCounter));
                    System.out.println("QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ");
                    ipCounter++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("333333333333");
            try {
                rs = stmt.executeQuery(sqlForServerIP);
                ipCounter = 0;
                while (rs.next()) {
                    serverIP.add(rs.getString("ip")); 
                    ipCounter++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("2222222222222222");
            try {
                rs = stmt.executeQuery(sqlForPrinterIP);
                ipCounter = 0;
                while (rs.next()) {
                    printerIP.add(rs.getString("ip")); 
                    ipCounter++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                System.out.println("444444444444444444");
                System.out.println(switchIP.size());
                if (!(switchIP.size() == 0)) {
                    for (int i = 0; i < switchIP.size(); i++) {
                        //final IcmpPingRequest request = IcmpPingUtil.createIcmpPingRequest();
                        InetAddress inet = InetAddress.getByName(switchIP.get(i));
                        boolean status = inet.isReachable(5000);
                        System.out.println(switchIP.get(i));
                        //request.setHost(switchAnotherIP.get(i));
                        //final IcmpPingResponse response = IcmpPingUtil.executePingRequest(request);
                        //final String formattedResponse = IcmpPingUtil.formatResponse(response);
                        //System.out.println("For " + switchAnotherIP.get(i) + "-->" + formattedResponse);
                        //System.out.println("Switch_" + switchAnotherIP.get(i));
                        if (status) {
                            mxCell myCell = (mxCell) ((mxGraphModel) graph.getModel()).getCell("Switch_" + switchIP.get(i));
                            mxGeometry cellGeometry = myCell.getGeometry();
                            Object Cell = ((mxGraphModel) graph.getModel()).getCell("Switch_" + switchIP.get(i));
                            graph.refresh();

                            myCell.setStyle("image;image=/com/mxgraph/examples/swing/images/SwitchIconRed.png;" + mxConstants.STYLE_OPACITY + "=50f;" + mxConstants.STYLE_FONTCOLOR + "=" + mxUtils.hexString(Color.RED));
                            System.out.println("myCell.getStyle():" + myCell.getStyle());
                            graph.refresh();
                            //Thread.sleep(5000);
                        } else {
                            System.out.println(switchIP.get(i) + "is reachable");
                        }
                    }
                }
            } catch (Exception e) {
               System.out.println("ERROR");
                e.printStackTrace();
            }
            System.out.println("TRY");
            try {
                System.out.println("111111111111");
                if ((!(switchAnotherIP.size() == 0))) {
                    for (int i = 0; i < switchAnotherIP.size(); i++) {
                        mxCell myCell = (mxCell) ((mxGraphModel) graph.getModel()).getCell("Switch_" + switchAnotherIP.get(i));
                        //final IcmpPingRequest request = IcmpPingUtil.createIcmpPingRequest();
                         InetAddress inet = InetAddress.getByName(switchAnotherIP.get(i));
                          boolean status = inet.isReachable(8000);
                        System.out.println(switchAnotherIP.get(i));
                        //request.setHost(switchAnotherIP.get(i));
                        //final IcmpPingResponse response = IcmpPingUtil.executePingRequest(request);
                        //final String formattedResponse = IcmpPingUtil.formatResponse(response);
                        //System.out.println("For " + switchAnotherIP.get(i) + "-->" + formattedResponse);
                        //System.out.println("Switch_" + switchAnotherIP.get(i));
                        if (!status) {
                           //myCell = (mxCell) ((mxGraphModel) graph.getModel()).getCell("Switch_" + switchAnotherIP.get(i));
                            //mxGeometry cellGeometry = myCell.getGeometry();
                            //Object Cell = ((mxGraphModel) graph.getModel()).getCell("Switch_" + switchAnotherIP);
                            graph.refresh();
                            System.out.println(mxConstants.STYLE_OPACITY);
                            myCell.setStyle("image;image=/com/mxgraph/examples/swing/images/SwitchIconRed.png;" + mxConstants.STYLE_OPACITY + "=50f;" + mxConstants.STYLE_FONTCOLOR + "=" + mxUtils.hexString(Color.RED));
                            System.out.println("myCell.getStyle():" + myCell.getStyle());
                            graph.refresh();
                            Thread.sleep(5000);
                        } else {
                            if(myCell.getStyle().contains("=50f"))
                            {
                            myCell.setStyle("image;image=/com/mxgraph/examples/swing/images/SwitchIcon.png");
                            }
                            System.out.println(switchAnotherIP.get(i) + "is reachable");
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if ((!(serverIP.size() == 0))) {
                    for (int i = 0; i < serverIP.size(); i++) {
                        //final IcmpPingRequest request = IcmpPingUtil.createIcmpPingRequest();
                         InetAddress inet = InetAddress.getByName(serverIP.get(i));
                          boolean status = inet.isReachable(5000);
                        System.out.println(serverIP.get(i));
                        //request.setHost(switchAnotherIP.get(i));
                        //final IcmpPingResponse response = IcmpPingUtil.executePingRequest(request);
                        //final String formattedResponse = IcmpPingUtil.formatResponse(response);
                        //System.out.println("For " + switchAnotherIP.get(i) + "-->" + formattedResponse);
                        //System.out.println("Switch_" + switchAnotherIP.get(i));
                        if (status) {
                            mxCell myCell = (mxCell) ((mxGraphModel) graph.getModel()).getCell("Switch_" + serverIP.get(i));
                            mxGeometry cellGeometry = myCell.getGeometry();
                            Object Cell = ((mxGraphModel) graph.getModel()).getCell("Switch_" + serverIP.get(i));
                            graph.refresh();
                            System.out.println(mxConstants.STYLE_OPACITY);
                            myCell.setStyle("image;image=/com/mxgraph/examples/swing/images/ServerRed.png;" + mxConstants.STYLE_OPACITY + "=50f;" + mxConstants.STYLE_FONTCOLOR + "=" + mxUtils.hexString(Color.RED));
                            graph.refresh();
                            Thread.sleep(5000);
                        } else {
                            System.out.println(serverIP.get(i) + "is reachable");
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if ((!(printerIP.size() == 0))) {
                    for (int i = 0; i < printerIP.size(); i++) {
                         //final IcmpPingRequest request = IcmpPingUtil.createIcmpPingRequest();
                         InetAddress inet = InetAddress.getByName(printerIP.get(i));
                          boolean status = inet.isReachable(5000);
                        System.out.println(printerIP.get(i));
                        //request.setHost(switchAnotherIP.get(i));
                        //final IcmpPingResponse response = IcmpPingUtil.executePingRequest(request);
                        //final String formattedResponse = IcmpPingUtil.formatResponse(response);
                        //System.out.println("For " + switchAnotherIP.get(i) + "-->" + formattedResponse);
                        //System.out.println("Switch_" + switchAnotherIP.get(i));
                        if (status) {
                            mxCell myCell = (mxCell) ((mxGraphModel) graph.getModel()).getCell("Switch_" + printerIP.get(i));
                            mxGeometry cellGeometry = myCell.getGeometry();
                            Object Cell = ((mxGraphModel) graph.getModel()).getCell("Switch_" + printerIP.get(i));
                            graph.refresh();
                            System.out.println(mxConstants.STYLE_OPACITY);
                            myCell.setStyle("image;image=/com/mxgraph/examples/swing/images/PrinterRed.png;" + mxConstants.STYLE_OPACITY + "=50f;" + mxConstants.STYLE_FONTCOLOR + "=" + mxUtils.hexString(Color.RED));
                            graph.refresh();
                            Thread.sleep(5000);
                        } else {
                            System.out.println(ArrayListofVisibleItem.arrlist.get(i) + "is reachable");
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if ((!(routerIP.size() == 0))) {
                    for (int i = 0; i < routerIP.size(); i++) {
                        //final IcmpPingRequest request = IcmpPingUtil.createIcmpPingRequest();
                         InetAddress inet = InetAddress.getByName(routerIP.get(i));
                          boolean status = inet.isReachable(5000);
                        System.out.println(routerIP.get(i));
                        //request.setHost(switchAnotherIP.get(i));
                        //final IcmpPingResponse response = IcmpPingUtil.executePingRequest(request);
                        //final String formattedResponse = IcmpPingUtil.formatResponse(response);
                        //System.out.println("For " + switchAnotherIP.get(i) + "-->" + formattedResponse);
                        //System.out.println("Switch_" + switchAnotherIP.get(i));
                        if (status) {
                           mxCell myCell = (mxCell) ((mxGraphModel) graph.getModel()).getCell("Switch_" + routerIP.get(i));
                            mxGeometry cellGeometry = myCell.getGeometry();
                            Object Cell = ((mxGraphModel) graph.getModel()).getCell("Switch_" + routerIP.get(i));
                            graph.refresh();
                            System.out.println(mxConstants.STYLE_OPACITY);
                            myCell.setStyle("image;image=/com/mxgraph/examples/swing/images/RouterRed.png;" + mxConstants.STYLE_OPACITY + "=50f;" + mxConstants.STYLE_FONTCOLOR + "=" + mxUtils.hexString(Color.RED));

                            graph.refresh();
                            Thread.sleep(5000);
                        } else {
                            System.out.println(routerIP.get(i) + "is reachable");
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if ((!(firewallIP.size() == 0))) {
                    for (int i = 0; i < firewallIP.size(); i++) {
                         //final IcmpPingRequest request = IcmpPingUtil.createIcmpPingRequest();
                         InetAddress inet = InetAddress.getByName(firewallIP.get(i));
                          boolean status = inet.isReachable(5000);
                        System.out.println(firewallIP.get(i));
                        //request.setHost(switchAnotherIP.get(i));
                        //final IcmpPingResponse response = IcmpPingUtil.executePingRequest(request);
                        //final String formattedResponse = IcmpPingUtil.formatResponse(response);
                        //System.out.println("For " + switchAnotherIP.get(i) + "-->" + formattedResponse);
                        //System.out.println("Switch_" + switchAnotherIP.get(i));
                        if (status) {
                            mxCell myCell = (mxCell) ((mxGraphModel) graph.getModel()).getCell("Switch_" + firewallIP.get(i));
                            mxGeometry cellGeometry = myCell.getGeometry();
                            Object Cell = ((mxGraphModel) graph.getModel()).getCell("Switch_" + firewallIP.get(i));
                            graph.refresh();
                            System.out.println(mxConstants.STYLE_OPACITY);
                            myCell.setStyle("image;image=/com/mxgraph/examples/swing/images/FirewallIcon.png;" + mxConstants.STYLE_OPACITY + "=50f;" + mxConstants.STYLE_FONTCOLOR + "=" + mxUtils.hexString(Color.RED));
                            graph.refresh();
                            Thread.sleep(5000);
                            //Thread.
                        } else {
                            System.out.println(ArrayListofVisibleItem.arrlist.get(i) + "is reachable");
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    } //catch (final Throwable t) {
    // log
    // t.printStackTrace();
    //}
    //}
    // TODO Auto-generated method stub
}
